﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace ReportWebApi.Filters
{
    [AttributeUsage(AttributeTargets.Class, Inherited = true)]
    public class BasicActionFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            base.OnActionExecuting(actionContext);
        }

        /// <summary>
        /// 拦截返回，加上跨域
        /// </summary>
        /// <param name="actionExecutedContext"></param>
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            HttpRequestMessage req = actionExecutedContext.Request;
            HttpResponseMessage response = actionExecutedContext.Response ?? actionExecutedContext.Request.CreateResponse();

            string Origin = "Origin";

            if (req.Headers.Contains(Origin))
            {
                string originUrl = req.Headers.GetValues(Origin).FirstOrDefault();
                response.Headers.Add("Access-Control-Allow-Origin", originUrl);
            }

            //  response.Headers.Add("Access-Control-Allow-Credentials", "true");

            base.OnActionExecuted(actionExecutedContext);
        }
    }
}